﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace WPFExcelReport
{
    public partial class ViewBase
    {
        #region ==IsLogOn ===========
        public static readonly DependencyProperty IsLogOnProperty = DependencyProperty.Register("IsLogOn",
            typeof(bool), typeof(ViewBase),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsLogOnChanged));
        public bool IsLogOn
        {
            get
            {
                return app.IsLogOn;
            }
            set
            {
                SetValue(IsLogOnProperty, value);

            }
        }
        private static void OnIsLogOnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ViewBase;
            if (src != null)
            {
                src.IsLogOn = (bool)e.NewValue;
                src.OnLogOnChg((bool)e.NewValue);
            }
        }
        public virtual void OnLogOnChg(bool value)
        {
            if (MV == null)
                return;
            app.IsLogOn = value;
        }
        #endregion
        private void AllPossible()
        {
            IsCanDelete = true;
            IsCanNew = true;
            IsCanChg = true;
            IsCanCopy = true;
            IsCanManage = true;
            ScopeManage = "*";
        }
        /// <summary>
        /// 为控件指定权限，界面是否使用权限控制，由界面决定
        /// </summary>
        public virtual void SetUpUserRight()
        {
            AllPossible();
            if (MV == null)
            {
                ScopeManage = "*";
            }
            string filter = "USERID='" + app.User + "' AND PROG='" + ("T" + MV.TableName).PadRight(80) + "'";
            if (TBUR == null) return;

            DataRow[] rows = TBUR.Select(filter);
            if (rows.Length == 0)
            {
                return;
            }
            foreach (DataRow row in rows)
            {
                string contol = row["CONTROLS"].ToString();//属性 
                string PROCESS = row["PROCESS"].ToString();//控制值
                string txt = row["P0"].ToString();//文本值 
                bool blControl = true;
                if (PROCESS.Trim() == "N")
                {
                    blControl = false; ;
                }
                if (PROCESS.Trim() == "Y")
                {
                    blControl = true; ;
                }
                switch (contol.Trim())
                {
                    case "HLD":
                        IsCanInactive = blControl;
                        break;
                    case "DEL":
                        IsCanDelete = blControl;
                        break;
                    case "NEW":
                        IsCanNew = blControl;
                        break;
                    case "COPY":
                        IsCanCopy = blControl;
                        break;
                    case "CHG":
                        IsCanChg = blControl;
                        break;
                    case "CTRL":
                        IsCanManage = blControl;
                        break;
                    case "MS":
                        ScopeManage = txt;
                        break;
                }
            }
        }
    }
}
